package view.common;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class ButtonRenderer extends JPanel implements TableCellRenderer {
    private JButton editButton;
    private JButton deleteButton;
    
    public ButtonRenderer() {
        setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
        editButton = new JButton("编辑");
        deleteButton = new JButton("删除");
        
        // 设置按钮的首选大小
        Dimension buttonSize = new Dimension(60, 25);
        editButton.setPreferredSize(buttonSize);
        deleteButton.setPreferredSize(buttonSize);
        
        editButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        deleteButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        
        // 设置按钮不获取焦点
        editButton.setFocusable(false);
        deleteButton.setFocusable(false);
        
        // 设置面板不透明
        setOpaque(true);
        
        add(editButton);
        add(deleteButton);
    }
    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        editButton.setEnabled(true);
        deleteButton.setEnabled(true);
        
        // 重置按钮的背景色和边框
        editButton.setBackground(UIManager.getColor("Button.background"));
        deleteButton.setBackground(UIManager.getColor("Button.background"));
        editButton.setBorderPainted(true);
        deleteButton.setBorderPainted(true);
        
        return this;
    }
} 